from spark.core import run_analysis
from utils.orm_base import OrmData
from spark.analyze_models import analyzed_room
from spark.models import LiveRoom
import asyncio


async def add_orm(room_id, analyzed_date):
    # 初始化数据库连接
    orm = OrmData()
    await orm.init()

    await analyzed_room.create(room_id=room_id, analyzed_date=analyzed_date)

    # 关闭数据库连接
    await orm.close()


async def get_analyzed():
    # 初始化数据库连接
    orm = OrmData()
    await orm.init()

    result_list = await analyzed_room.all().values_list('room_id', 'analyzed_date')
    analyzed_list = [dict(room_id=room_id, analyzed_date=analyzed_date)
                     for room_id, analyzed_date in result_list]

    result_list = await LiveRoom.all().values_list('room_id', 'saved_date')
    saved_list = [dict(room_id=room_id, saved_date=saved_date)
                  for room_id, saved_date in result_list]

    # 关闭数据库连接
    await orm.close()

    return analyzed_list


async def get_saved():
    # 初始化数据库连接
    orm = OrmData()
    await orm.init()

    result_list = await LiveRoom.all().values_list('room_id', 'saved_date')
    saved_list = [dict(room_id=room_id, saved_date=saved_date)
                  for room_id, saved_date in result_list]

    # 关闭数据库连接
    await orm.close()

    return saved_list


def get_filtered():
    analyzed_list = asyncio.run(get_analyzed())
    saved_list = asyncio.run(get_saved())
    # 使用列表推导式过滤出saved_list中不包含在analyzed_list中的元素
    filtered_list = [
        item for item in saved_list
        if item['room_id'] not in [a['room_id'] for a in analyzed_list]
        or item['saved_date'] not in [a['analyzed_date'] for a in analyzed_list]
    ]
    return filtered_list


def add_analyzed(room_id, analyzed_date):
    asyncio.run(add_orm(room_id, analyzed_date))


if __name__ == '__main__':
    filtered_list = get_filtered()
    print("未分析数据：")
    print(filtered_list)
    type_list = ["ChatMessage", "GiftMessage",
                 "LikeMessage", "MemberMessage", "SocialMessage"]
    for item in filtered_list:
        room_id = item['room_id']
        analyze_time = item['saved_date']
        for table_type in type_list:
            # 分析直播间互动情况
            run_analysis(room_id, analyze_time, table_type,
                         analysis_type="frequency_analysis")
            # 分析直播间男女占比情况
            run_analysis(room_id, analyze_time, table_type,
                         analysis_type="gender_analysis")

        # 分析用户行为
        run_analysis(room_id, analyze_time, "ChatMessage",
                     analysis_type="water_army_analysis")
        add_analyzed(room_id, analyze_time)
